<?php


namespace api\zmt\controller;


use api\zmt\model\AddModel;

use api\zmt\model\UserModel;
use app\admin1\validate\AddValidate;
use cmf\controller\RestBaseController;
use think\exception\Handle;
use think\Request;

class AddressController extends RestBaseController
{
    //查询收货地址
    public function address(){
        $address = AddModel::field('consignee,mobile,area,address')
            ->select();
        $this->success('个人信息查询项成功',$address);
    }
    //添加收货地址
    public function add(Request $request){
        $data = input('post.','');
        $data['user_id'] =  $request->userId;
//        halt($data['user_id']);
        $validate = new AddValidate();
        $result = $validate->scene('add')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }
//        request()->data->user_id;
//        $request->userId = $res->data->user_id;

        if(AddModel::create($data)){
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
    }
    //修改地址
    public function edit(Request $request){
        $data = input('post.','');
        $data['user_id'] =  $request->userId;
//        halt($data['user_id']);
        $validate = new AddValidate();
        $result = $validate->scene('edit')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }
        //查询用户是否存在
        $user = AddModel::find($data['id']);
        if(!$user){
            $this->error('该收货地址不存在');
        }
        if(AddModel::update($data)){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
    //删除地址
    public function delete(Request $request){
        $data = input('get.');
        $data['user_id'] =  $request->userId;
        if(!$data['id']){
            $this->error('参数错误');
        }
        if(AddModel::destroy($data['id'])){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
}